using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Integrator.DAL;
using Integrator.DAL.MESSAGE;
using System.Threading;
using NLog;

/// <summary>
/// Summary description for Logger
/// </summary>
public class SystemLogger
{
    private static Logger log = LogManager.GetCurrentClassLogger();

    public SystemLogger()
    {
        
    }

    public void LogRequest(BaseRequest request)
    {
        Thread thread = new Thread(delegate() { LogMessage(request); });
        thread.Start();
    }

    public void LogResponse(BaseResponse response)
    {
        Thread thread = new Thread(delegate() { LogMessage(response); });
        thread.Start();
    }

    private void LogMessage(BaseMessage message)
    {
        try
        {
            string remoteIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            AccessLog accessLog = new AccessLog();
            accessLog.UpdateTime = DateTime.Now;
            accessLog.ClientAddress = remoteIp;
            accessLog.ClientType = "";
            accessLog.MessageContent = message.ToString();
            accessLog.MessageName = message.GetType().Name;
            new SqlHelper().UpdateLog(accessLog);
        }
        catch (Exception ex)
        {
            log.ErrorException("", ex);
        }
    }
}
